package service

import (
	"backend/curd"
	"backend/databases"
	"backend/define"
	"backend/models"
	"backend/utils"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// 获取管理员列表数据
func GetUserList(c *gin.Context) {
	in := &GetUserRequestList{NewQueryRequest()}
	err := c.ShouldBindQuery(in)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "参数异常",
		})
		return
	}

	var (
		cnt  int64
		list = make([]*GetUserListReply, 0)
	)

	err = curd.GetUserList(in.Keyword).Count(&cnt).Offset((in.Page - 1) * in.Size).Limit(in.Size).Find(&list).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "系统异常",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "操作成功",
		"result": gin.H{
			"list":  list,
			"count": cnt,
		},
	})
}

// 新增管理员信息
func AddUser(c *gin.Context) {
	in := new(AddUserRequest)
	err := c.ShouldBindJSON(in)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "参数异常",
		})
		return
	}
	// 校验用户名
	validate_username := utils.ValidateUsername(in.UserName)
	validate_password := utils.ValidatePassword(in.Password)
	if validate_username == false {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "用户名必须为3到20个字母或数字",
		})
		return
	} else if validate_password == false {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "密码必须是8到20长度的大小写、特殊字符和数字组成",
		})
		return
	} else {

		var cnt int64
		err = databases.DB.Model(new(models.SysUser)).Where("username = ?", in.UserName).Count(&cnt).Error

		// cnt大于0 用户已存在
		if cnt > 0 {
			c.JSON(http.StatusOK, gin.H{
				"code": -1,
				"msg":  "用户名已存在",
			})
			return
		}

		// 校验密码
		Password, err := utils.GenerateFromPassword(in.Password)
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": -1,
				"msg":  "系统错误",
			})
			return
		}
		err = databases.DB.Create(&models.SysUser{
			Username: in.UserName,
			Password: Password,
			Phone:    in.Phone,
			Remark:   in.Remark,
			RoleId:   in.RoleId,
		}).Error
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": -1,
				"msg":  "系统异常",
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "操作成功",
		})
	}

}

// 根据id获取管理员的详细信息
func GetUserDetail(c *gin.Context) {
	in := new(GetUserDetailRequest)
	err := c.ShouldBindJSON(in)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "系统错误",
		})
		return
	}
	id := strconv.FormatUint(uint64(in.ID), 10)
	if id == "" {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "参数错误",
		})
		return
	}
	uId, err := strconv.Atoi(id)
	data := new(GetUserDetailReply)
	// 获取管理员信息
	sysUser, err := curd.GetUserDetail(uint(uId))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "系统错误",
		})
		return
	}
	data.ID = sysUser.ID
	data.UserName = sysUser.Username
	data.Phone = sysUser.Phone
	data.Remark = sysUser.Remark
	data.Password = sysUser.Password
	data.RoleId = sysUser.RoleId
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "OK",
		"data": data,
	})

}

// 修改管理员信息
func UpdateUser(c *gin.Context) {
	in := new(UpdateUserRequest)
	err := c.ShouldBindJSON(in)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "参数错误",
		})
		return
	}
	if utils.ValidateUsername(in.UserName) == false {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "用户名必须是3到20的字母或数字",
		})
		return
	} else if in.Password != "" && utils.ValidatePassword(in.Password) == false {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "密码必须是长度为8到20的大小写字母、数字和特殊字符组合而成",
		})
		return
	} else {
		// 判断用户是否存在
		var cnt int64
		err = databases.DB.Model(new(models.SysUser)).Where("id !=? AND username = ?", in.ID, in.UserName).Count(&cnt).Error
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": -1,
				"msg":  "系统异常",
			})
			return
		}
		if cnt > 0 {
			c.JSON(http.StatusOK, gin.H{
				"code": -1,
				"msg":  "用户已存在",
			})
			return
		}
		password, err := utils.GenerateFromPassword(in.Password)
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": -1,
				"msg":  "系统错误",
			})
		}
		// 修改数据
		err = databases.DB.Model(new(models.SysUser)).Where("id = ?", in.ID).Updates(map[string]any{
			"password": password,
			"username": in.UserName,
			"phone":    in.Phone,
			"remark":   in.Remark,
			"role_id":  in.RoleId,
		}).Error
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": -1,
				"msg":  "系统错误",
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "OK",
		})
	}
}

// 删除管理员
func DeleteUser(c *gin.Context) {
	in := new(DeleteUserRequest)
	err := c.ShouldBindJSON(in)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "系统错误",
		})
		return
	}
	id := strconv.FormatUint(uint64(in.ID), 10)
	err = databases.DB.Where("id = ?", id).Delete(new(models.SysUser)).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "系统错误",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "OK",
	})
}

// 跟新个人密码
func UpdateUserPassword(c *gin.Context) {
	// 获取用户信息
	userClaim := c.MustGet("userClaim").(*define.UserClaims)
	in := new(UpdatePwdRequest)
	err := c.ShouldBindJSON(in)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "更新失败, 密码信息不能为空",
		})
		return
	}
	// 根据用户id获取用户信息
	sysUser, err := curd.GetUserDetail(userClaim.Id)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "更新失败，用户不存在",
		})
		return
	}
	// 判断输入的旧密码是否正确
	if sysUser.Password != in.UsedPassword {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "输入的旧密码不正确",
		})
		return
	}
	// 更新数据
	err = databases.DB.Model(new(models.SysUser)).Where("id = ?", userClaim.Id).Updates(map[string]any{
		"password": in.UsedPassword,
	}).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "更新失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "OK",
	})
}
